package com.leyou.item.web;

import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.service.CategoryService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName : CategoryController  //类名
 * @Description :   //描述
 * @Author : qingfeng  //作者
 * @Date: 2020-09-15 15:44  //时间
 */
@RestController
@RequestMapping("category")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService){
        this.categoryService = categoryService;
    }

    /**
     * 根据id查询分类
     * @param id 分类id
     * @return 分类对象
     */
    @GetMapping("{id}")
    public ResponseEntity<CategoryDTO> queryCategoryById(@PathVariable("id") Long id){
        return ResponseEntity.ok(new CategoryDTO(categoryService.getById(id)));
    }

    @GetMapping("list")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByIds(@RequestParam("ids")List<Long> ids){
        /**
         * @Description //TODO 根据id集合查询分类
         * @Param [ids]
         * @return org.springframework.http.ResponseEntity<java.util.List < com.leyou.item.dto.CategoryDTO>>
         */
        return ResponseEntity.ok(CategoryDTO.convertEntityList(categoryService.listByIds(ids)));
    }

    @GetMapping("/of/parent")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByParentId(@RequestParam("pid")Long pid){
        return ResponseEntity.ok(CategoryDTO.convertEntityList(categoryService.query().eq("parent_id",pid).list()));
    }

    /**
     * 根据品牌id查询分类集合
     * @param brandId 品牌id
     * @return 分类集合
     */
    @GetMapping("/of/brand")
    public ResponseEntity<List<CategoryDTO>> queryCategoryByBrandId(@RequestParam("id") Long brandId){
        return ResponseEntity.ok(categoryService.queryCategoryByBrandId(brandId));
    }
}
